﻿using GH_IO.Serialization;
using Grasshopper.Kernel.Types;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace D003_Param
{
    public class GH_Human : GH_Goo<Human>
    {
        private class GH_HumanProxy : GH_GooProxy<GH_Human>
        {
            [Description("人的名字")]
            [DisplayName("姓名")]
            [Category("信息")]
            [RefreshProperties(RefreshProperties.All)]
            public string Name
            {
                get
                {
                    return Owner.Value.Name;
                }
                set
                {
                    Owner.Value.Name = value;
                }
            }

            [Description("人的名字")]
            [DisplayName("姓名")]
            [Category("信息")]
            [RefreshProperties(RefreshProperties.All)]
            public int Age
            {
                get
                {
                    return Owner.Value.Age;
                }
                set
                {
                    Owner.Value.Age = value;
                }
            }

            [Description("人的名字")]
            [DisplayName("姓名")]
            [Category("信息")]
            [RefreshProperties(RefreshProperties.All)]
            public int Id
            {
                get
                {
                    return Owner.Value.Id;
                }
            }
            public GH_HumanProxy(GH_Human owner) : base(owner)
            {
            }
        }
        public override bool IsValid => true;

        public override string TypeName => nameof(Human);

        public override string TypeDescription => "这是一个关于人的类型";

        public GH_Human(GH_Human other) : base(other)
        {

        }
        public GH_Human()
        {
            Value = new Human();
        }

        public override IGH_Goo Duplicate()
        {
            return new GH_Human(this);
        }

        public override string ToString()
        {
            return $"姓名：{Value.Name}\n年龄：{Value.Age}\nID：{Value.Id}";
        }

        public override bool Write(GH_IWriter writer)
        {
            writer.SetString(nameof(Value.Name), Value.Name);
            writer.SetInt32(nameof(Value.Age), Value.Age);
            writer.SetInt32(nameof(Value.Id), Value.Id);
            return base.Write(writer);
        }

        public override bool Read(GH_IReader reader)
        {
            string name = string.Empty;
            if (reader.TryGetString(nameof(Value.Name), ref name))
            {
                Value.Name = name;
            }

            int age = 0;
            if (reader.TryGetInt32(nameof(Value.Age), ref age))
            {
                Value.Age = age;
            }

            int id = 0;
            if (reader.TryGetInt32(nameof(Value.Id), ref id))
            {
                Value.Id = id;
            }


            return base.Read(reader);
        }

        public override bool CastFrom(object source)
        {
            if (source.GetType().IsAssignableFrom(typeof(string)))
            {
                Value = new Human() { Name = (string)source };
                return true;
            }
            else if (source is int id)
            {
                Value = new Human() { Id = id };
                return true;
            }

            return base.CastFrom(source);
        }

        public override bool CastTo<Q>(ref Q target)
        {
            if (typeof(Q).IsAssignableFrom(typeof(string)))
            {
                target = (Q)(object)Value.Name;
                return true;
            }
            else if (typeof(Q).IsAssignableFrom(typeof(int)))
            {
                target = (Q)(object)Value.Id;
                return true;
            }
            return base.CastTo(ref target);
        }

        public override IGH_GooProxy EmitProxy()
        {
            return new GH_HumanProxy(this);
        }
    }
}
